<?php	
	ini_set("display_errors","2");	
	//interfaces 讀取csv的介面
	require_once("CsvInterface.class.php");
	//classes 讀取csv類別
	require_once("Csv.class.php");
	require_once "inc/common.php";
	/**讀取csv並且寫入資料庫(尚未處理重複資料)
	 * 
	 * @return bool
	 */	
class Tools
{
	public static function processCSV($csvName='')
	{
		global $path;
		/**
		 * 
		 * @var Csv
		 */
		//$csv = new Csv($path."uploads$csvName", $separator = ",");
		$csv = new Csv("uploads/$csvName", $separator = ",");		
		$data = $csv->readAll();
		/**
		 * 
		 * @var ServiceHour
		 */	
		$service = new ServiceHour();
		$dataCount = count($data);
		$c = new Criteria();
		/**
		 * 讀取csv資料數量
		 */
		for($i=0;$i<$dataCount;$i++)
		{
			$schYear = $data[$i][0];
			$clas = iconv("big5","utf-8",$data[$i][1]);
			$name = iconv("big5","utf-8",$data[$i][2]);
			$agent = iconv("big5","utf-8",$data[$i][3]);
			$startTime = $data[$i][4];
			$endTime = $data[$i][5];
			$hour = $data[$i][6];
			$c->add(ServiceHourPeer::SCH_YEAR,$schYear);
			$c->add(ServiceHourPeer::CLAS,$clas);
			$c->add(ServiceHourPeer::NAME,$name);
			$c->add(ServiceHourPeer::AGENT,$agent);
			$c->add(ServiceHourPeer::START_TIME,$startTime);
			$c->add(ServiceHourPeer::END_TIME,$endTime);
			$c->add(ServiceHourPeer::HOUR,$hour);
			try
			{
				ServiceHourPeer::doInsert($c);			
			}
			catch(PropelException $e)
			{						
			}
		}			
	}
	/**
	 * 
	 * @return string
	 */
	public static function getServiceHourData()
	{
		$c = new Criteria();
		$serviceObjAry = ServiceHourPeer::doSelect($c);
		$dataCount = count($serviceObjAry);
		$html="<table>\n".
		      "<tr><th>學年度</th><th>班級</th><th>姓名</th><th>機構名稱</th><th>開始服務時間</th><th>結束服務時間</th><th>服務時數</th>\n".
		      "</tr>\n";		
		for($i=0;$i<$dataCount;$i++)
		{	
			$html.="<tr>\n";		
			$schYear = $serviceObjAry[$i]->getSchYear();
			$clas = $serviceObjAry[$i]->getClas();
			$name = $serviceObjAry[$i]->getName();
			$agent = $serviceObjAry[$i]->getAgent();
			$startTime = $serviceObjAry[$i]->getStartTime();
			$endTime = $serviceObjAry[$i]->getEndTime();
			$hour = $serviceObjAry[$i]->getHour();
			$html.="<td>$schYear</td>\n";
			$html.="<td>$clas</td>\n";
			$html.="<td>$name</td>\n";
			$html.="<td>$agent</td>\n";
			$html.="<td>$startTime</td>\n";
			$html.="<td>$endTime</td>\n";
			$html.="<td>$hour</td>\n";
			$html.="</tr>\n";					
		}
		$html.="</table>";
		return $html;
	}
	/**取得2d陣列資料大小
	 * 
	 * @param $data
	 * @return unknown_type
	 */
	public function getFileSize($data)
	{	
		$size = 0;
		$counts = count($data);
		for($i=0;$i<$counts;$i++)
		{
			$size+=$i;
			for($j=0;$j<count($data[$i]);$j++)
			{			
				$size+=$j;	
			}			
		}
		return $size;
	}
}
?>